package com.yiwei.zhh.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.base.ItemAdapter;
import com.yiwei.zhh.model.BankCardListModel;
import com.yiwei.zhh.model.BankListModel;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BankCardSelectAdapter extends ItemAdapter<BankCardListModel.DataBean, BankCardSelectAdapter.DefaultHolder> {

    private int[] bankImg = {R.mipmap.bank_default, R.mipmap.guangfa, R.mipmap.huaxia, R.mipmap.shanghai, R.mipmap.xingye, R.mipmap.zhaoshang, R.mipmap.gongshang, R.mipmap.nongye, R.mipmap.zhongguoyinhang, R.mipmap.zhongxin, R.mipmap.minsheng, R.mipmap.jianshe, R.mipmap.guangda, R.mipmap.pufa, R.mipmap.youzheng, R.mipmap.nongcun, R.mipmap.jiaotong};
    private Context context;
    private boolean isSerViceFee = false;

    public BankCardSelectAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public void setSerViceFee(boolean isSerViceFee) {
        this.isSerViceFee = isSerViceFee;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_bank_card_select, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        BankCardListModel.DataBean item = getItem(position);
        if (item.bank.cut_support.equals("1")) {
            if (item.is_allow_cut.equals("1")) {

                holder.txtBank.setTextColor(0xff49506D);
                holder.txtSupport.setVisibility(View.GONE);
                holder.btnIdentity.setVisibility(View.GONE);
//                holder.imgCheck.setVisibility(View.VISIBLE);
            } else {

                holder.txtBank.setTextColor(0xff49506D);
                holder.txtSupport.setVisibility(View.GONE);
                holder.btnIdentity.setVisibility(View.VISIBLE);
//                holder.imgCheck.setVisibility(View.GONE);
            }

        } else {

            holder.txtBank.setTextColor(0xffCACACA);
            holder.txtSupport.setVisibility(View.VISIBLE);
            holder.btnIdentity.setVisibility(View.GONE);
//            holder.imgCheck.setVisibility(View.GONE);
        }
        holder.imgCheck.setVisibility(View.GONE);
        getBankImg(item.bank.bank_card_type, holder.imgIcon);
        holder.txtBank.setText(item.bank.bank_card_type + "(" + item.bankcard.substring(0, 3) + "****" + item.bankcard.substring(item.bankcard.length() - 4, item.bankcard.length()) + ")");
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (item.bank.cut_support.equals("1") && item.tg_is_sign.equals("1") && isSerViceFee) {
//                    if (shareClickListener != null) {
//                        shareClickListener.onClick(item.id, item.bankcard);
//                    }
//                } else if (item.is_allow_cut.equals("1") && item.bank.cut_support.equals("1") && item.tg_is_sign.equals("1")) {
//                    holder.imgCheck.setVisibility(View.VISIBLE);
//                    if (shareClickListener != null) {
//                        shareClickListener.onClick(item.id, item.bankcard);
//                    }
//                } else {
//                    if (item.bank.cut_support.equals("1")) {
//                        if (shareClickListener != null) {
//                            shareClickListener.goToBind(item.id, item.is_allow_cut, item.tg_is_sign, item.bankcard);
//                        }
//                    } else {
//                        ToastUtil.makeText(context, "暂不支持该卡");
//                    }
//                }
                if (item.bank.cut_support.equals("1") && !item.is_allow_cut.equals("1")) {
                    if (shareClickListener != null) {
                        shareClickListener.goToBind(item.id);
                    }
                    return;
                }


                if (item.is_allow_cut.equals("1") && item.bank.cut_support.equals("1")) {
                    if (shareClickListener != null) {
                        shareClickListener.onClick(item.id, item.bankcard);
                    }
                    holder.imgCheck.setVisibility(View.VISIBLE);
                }
            }
        });
    }


    private void getBankImg(String bankName, ImageView icon) {
        try {
            icon.setImageDrawable(ContextCompat.getDrawable(context, bankImg[0]));
            if (TextUtils.isEmpty(bankName)) return;
            int id = 0;
            if (bankName.contains(context.getString(R.string.guangfa))) id = 1;
            if (bankName.contains(context.getString(R.string.huaxia))) id = 2;
            if (bankName.contains(context.getString(R.string.shanghai))) id = 3;
            if (bankName.contains(context.getString(R.string.xingye))) id = 4;
            if (bankName.contains(context.getString(R.string.zhaoshang))) id = 5;
            if (bankName.contains(context.getString(R.string.gongshang))) id = 6;
            if (bankName.contains(context.getString(R.string.nongye))) id = 7;
            if (bankName.contains(context.getString(R.string.zhongguoyinhang)))
                id = 8;
            if (bankName.contains(context.getString(R.string.zhongxin))) id = 9;
            if (bankName.contains(context.getString(R.string.minsheng))) id = 10;
            if (bankName.contains(context.getString(R.string.jianshe))) id = 11;
            if (bankName.contains(context.getString(R.string.guangda))) id = 12;
            if (bankName.contains(context.getString(R.string.pufa))) id = 13;
            if (bankName.contains(context.getString(R.string.youzheng))) id = 14;
            if (bankName.contains(context.getString(R.string.nongcun))) id = 15;
            if (bankName.contains(context.getString(R.string.jiaotong))) id = 16;

            icon.setImageDrawable(ContextCompat.getDrawable(context, bankImg[id]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnShareClickListener {
        void onClick(String id, String bankcard);

        void goToBind(String id);
    }

    private OnShareClickListener shareClickListener;

    public void setShareClickListener(OnShareClickListener shareClickListener) {
        this.shareClickListener = shareClickListener;
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_identity)
        SuperButton btnIdentity;
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_bank)
        TextView txtBank;
        @BindView(R.id.img_check)
        ImageView imgCheck;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        @BindView(R.id.txt_support)
        TextView txtSupport;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
